約 5,047,413 件
https://w.atwiki.jp/norio_job/pages/17.html
・データベースの作成方法 $su - postgresユーザ $createdb データベース名 ・データベース接続方法 $psql データベース名
https://w.atwiki.jp/atwikimyj/pages/71.html
■PostgreSQLインストール 参考にしたサイト↓ postgresql.org http //www.postgresql.org/download/ postgresql.jp http //www.postgresql.jp/document/current/html/installation.html 環境 CentOS4.5 (RH4.5のフリー版ね) ※VMserver上で起動しております。 インストールは下記サイトを大いに参考にしました。 CentOS5 に PostgreSQL インストール http //108bones.blog71.fc2.com/ (1)ディレクトリの作成 /usr/local/postgresql (2)ユーザの作成 username postgresql ,pwd password (3)postgreSQLのダウンロード ※ftpやる前に、、ftpでgetしたデータは 現在のカレントディレクトリに保存されます。 なので先に保存したいディレクトリに移動してから ftpやったほうが面倒がなくて良い。 # ftp ftp.postgresql.org ftp name ここでanonymousでログイン ftp password ここで何も入力せずエンター 下記にファイルはあるのでcdコマンドで移動 ftp //ftp.postgresql.org/pub/v8.2.5/postgresql-8.2.5.tar.gz ftp get postgresql-8.2.5.tar.gz これでダウンロード完了 (4)解凍 # cd /usr/local/src # tar txvz postgresql-8.2.5 (5)インストール # cd postgresql-8.2.5 # ./configure 珍しくエラーなし。 # gmake # gmake install (6)環境変数の編集 /etc/profile をEMACSで編集。下記を追加 ※このファイルはbashにて操作する場合の 初期設定ファイル(シェルスクリプトで記述) LD_LIBRARY_PATH・・bash起動時にライブラリを見に行く パスを指定。pgsqlのライブラリをこれで使うことができる PATH ・・bashでのコマンド入力時にはPATH内に定義された 実行ファイルを探し、あったらそれを実行する。なので ソフトをインストールした際にはPATHを追加してやらんと いかん。 ************************************* LD_LIBRARY_PATH=/usr/local/pgsql/lib export LD_LIBRARY_PATH PATH=/usr/local/pgsql/bin $PATH export PATH MANPATH=/usr/local/pgsql/man $MANPATH export MANPATH ************************************* 一応ログオフ、ログインしなおす。 (7)DBの初期化(クラスタの作成) 何をしているかというと、pgSQLが使うデータの 保存先ディレクトリを指定して、そこにクラスタを 設定します。 単純に保存先の設定ではなくて、その保存先にはいろんな pgSQL独自のデータがないといけないのでそのデータの 作成もします。 initdb というコマンドを使う。 ************************************* $ su - postgres initdb -D /usr/local/pgsql/data ************************************* ※initdb --helpでいろいろとコマンドが確認できる。 initdb -D (path) initdb --pgdata=(path) 上記は同じ意味。 http //www.officeyano.net/loadpgsql.htm (8)pgSQLの起動と状態確認 pg_ctl start pgSQLを起動します。 pg_ctl status pgSQLの現在の状態を確認します。 (9)pgSQLの自動起動 サーバマシンが起動する時に自動的にPostgreSQLが 起動するようにします。Linux用に提供されている 設定ファイルを使います。 /etc/rc.d/init.d/ 内のスクリプトはOS起動時に自動的に読み込まれ、実行 されます。なのでこのディレクトリにpgSQL自動実行 スクリプトを保存します。 postgres という名前のファイルをコピーします。 (なのでpostgresという文字列をbashでコマンドとして 打つと、pgSQL起動スクリプトを実行しようとします。 通常、OS起動している状態であればすでに起動している ので起動中のpgSQLのプロセスIDが表示され、すでに 起動してるよ!と怒られます) ファイルのオーナーをpostgresにします。なぜならこの ユーザでないとpgSQLが起動できないからです。 あとランレベルも下記のように設定します(わからんけど) ************************************* # cd /etc/rc.d/init.d/ # cp -p /usr/local/src/postgresql-8.2.5/contrib/start-scripts/linux postgres # chown root.root postgres # chmod 755 postgres # chkconfig --level 2345 postgres on ************************************* (10)pgSQLでのデータベース操作 psql -d (dbname) データベースとの接続(この後コマンドで データベースを操作します。 psql -l 現在のデータベースのリストが表示されます。 (11)pgSQL起動後の操作 mydb=# \dt mydb内のテーブル一覧が出ます。 mydb=# \df mydb内のファンクション一覧が出ます。 とりあえずこれでOKとおもわれ。
https://w.atwiki.jp/c0metssd/pages/24.html
インストール 最新パッケージの確認 # yum search postgresql # yum install -y postgresql-server 最新が9.3(2014/7現在)が存在しないので、手動でダウンロードする https //www.enterprisedb.co.jp/products-services-training/pgdownload Linuxで起動 # chmod a+x postgresql-9.3.0-1-linux.run # ./postgresql-9.3.0-1-linux.run 画面に沿ってインストールする /opt/PostgreSQL/9.3 自動起動設定 # chkconfig postgresql-9.3 --list postgresql-9.3 0 off1 off2 on3 on4 on5 on6 off データベースの作り直し PG_DATAフォルダ以下を空にしないとinitdbできないので、rm -rfとかで消しておくこと。 # su - postgres $ rm -rf /opt/PostgreSQL/9.3/data/* $ /opt/PostgreSQL/9.3/bin/initdb -D /opt/PostgreSQL/9.3/data --encoding=UTF8 --no-locale 証明書の作成(オレオレ版) # su - # cd /opt/PostgreSQL/9.3/data # openssl genrsa -out server.key 2048 Generating RSA private key, 2048 bit long modulus ......+++ ...............................+++ e is 65537 (0x10001) # openssl req -new -key server.key -out server.csr You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter . , the field will be left blank. ----- Country Name (2 letter code) [XX] State or Province Name (full name) [] Locality Name (eg, city) [Default City] Organization Name (eg, company) [Default Company Ltd] Organizational Unit Name (eg, section) [] Common Name (eg, your name or your server s hostname) [] Email Address [] Please enter the following extra attributes to be sent with your certificate request A challenge password [] An optional company name [] # openssl x509 -days 3650 -req -signkey server.key server.csr server.crt Signature ok subject=/C=XX/L=Default City/O=Default Company Ltd Getting Private key パーミッションの変更 # chmod og-rwx server.key SSL設定 # vi postgresql.conf 以下の4行を追加 ssl = on ssl_ciphers = DEFAULT ssl_cert_file = server.crt ssl_key_file = server.key 再起動 # service postgresql-9.3 restart Restarting PostgreSQL 9.3 waiting for server to shut down.... done server stopped waiting for server to start.... done server started PostgreSQL 9.3 restarted successfully postgreSQLの設定 ユーザ作成 # useradd postgres # passwd postgres postgresにスイッチユーザする # su - postgres パスを設定しておく # vi ~/.bashrc 下記を追加 export PATH=$PATH /opt/PostgreSQL/9.3/bin/ ログインしなおして、パスの確認を行う。 -bash-4.1$ which createuser /opt/PostgreSQL/9.3/bin/createuser データベースアクセスユーザを作成 -bash-4.1$ createuser -U postgres -P hoge Enter password for new role hogeのパス Enter it again hogeのパス Password postgres データベース作成 -bash-4.1$ createdb -U postgres -O hogeuser -E UTF8 hogedb 作成確認 -bash-4.1$ psql -l データベース一覧 名前 | 所有者 | エンコーディング | 照合順序 | Ctype(変換演算子) | アクセス権 -----------+----------+------------------+-------------+-------------------+----------------------- hogedb | hogeuser | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | postgres | postgres | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | template0 | postgres | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/postgres postgres=CTc/postgres template1 | postgres | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/postgres postgres=CTc/postgres (4 行) 接続確認 -bash-4.1$ psql hogedb psql (8.4.20) "help" でヘルプを表示します. hogedb=# select * from user; current_user -------------- postgres (1 行) hogedb=# \q -bash-4.1$
https://w.atwiki.jp/duke15/pages/9.html
Database initialization root# mkdir /usr/local/pgsql/data root# chown postgres /usr/local/pgsql/data root# su postgres postgres$ initdb -D /usr/local/pgsql/data Starting DB $ postmaster -D /usr/local/pgsql/data logfile 2 1 pg_ctl start -l logfile add user postgres=# create user name password password ; CREATE ROLE postgres=# \du List of roles Role name | Superuser | Create role | Create DB | Connections | Member of -----------+-----------+-------------+-----------+-------------+----------- name | no | no | no | no limit | postgres | yes | yes | yes | no limit | (2 rows) Setup Copy JDBC driver Copy posgresql.jar to $JBOSS_HOME/server/default/lib Copy postgres-service.xml Copy $JBOSS_HOME/docs/examples/jca/postgres-service.xml to $JBOSS_HOME/server/default/deploy Modify postgres-service.xml
https://w.atwiki.jp/chapati4it/pages/43.html
Ubuntu server11.04上にインスールしたPostgreSqlに、C言語でアクセスしてみたいと思います。 以下の記事から、テストデータ作成、テストプログラムをとても参考にさせていただきました。 PostgreSQLをプログラムで操作する http //www.atmarkit.co.jp/flinux/rensai/postgres03/postgres03.html#3 環境構築 C言語からPostgreSqlを操作するにはlibpqというパッケージが必要みたい。 libpqで検索 $ sudo aptitude search libpq p libpq-dev - header files for libpq5 (PostgreSQL librar i A libpq5 - PostgreSQL C client library p libpqxx-2.6.9ldbl - C++ library to connect to PostgreSQL p libpqxx-2.6.9ldbl-dbg - C++ library to connect to PostgreSQL (debu p libpqxx-3.0 - C++ library to connect to PostgreSQL p libpqxx-3.0-dbg - C++ library to connect to PostgreSQL (debu p libpqxx-dev - C++ library to connect to PostgreSQL (deve p libpqxx-doc - C++ library to connect to PostgreSQL (docu p libpqxx3-dev - C++ library to connect to PostgreSQL (deve p libpqxx3-doc - C++ library to connect to PostgreSQL (docu libpq5がPostgreSqlと一緒にインストールされたみたい。 でも必要なのはきっとlibpq-dev libpq-devインストール $ sudo aptitude install libpq-dev ヘッダファイル確認 ディレクトリ /usr/include/postgresql/ に件のヘッダファイルがインストールされるみたい。 $ ll /usr/include/postgresql/ total 176 drwxr-xr-x 8 root root 4096 2011-09-29 20 51 ./ drwxr-xr-x 41 root root 4096 2011-09-29 20 51 ../ drwxr-xr-x 2 root root 4096 2011-09-29 20 51 catalog/ -rw-r--r-- 1 root root 24635 2012-06-04 13 09 c.h drwxr-xr-x 3 root root 4096 2011-09-29 20 51 internal/ drwxr-xr-x 2 root root 4096 2011-09-29 20 51 libpq/ -rw-r--r-- 1 root root 2270 2012-06-04 13 09 libpq-events.h -rw-r--r-- 1 root root 19646 2012-06-04 13 09 libpq-fe.h drwxr-xr-x 2 root root 4096 2011-09-29 20 51 mb/ drwxr-xr-x 2 root root 4096 2011-09-29 20 51 nodes/ -rw-r--r-- 1 root root 24844 2012-06-04 13 09 pg_config.h -rw-r--r-- 1 root root 7583 2012-06-04 13 09 pg_config_manual.h -rw-r--r-- 1 root root 1111 2012-06-04 13 09 pg_config_os.h -rw-r--r-- 1 root root 349 2012-06-04 13 09 pg_trace.h -rw-r--r-- 1 root root 13126 2012-06-04 13 09 port.h -rw-r--r-- 1 root root 1837 2012-06-04 13 09 postgres_ext.h -rw-r--r-- 1 root root 790 2012-06-04 13 09 postgres_fe.h -rw-r--r-- 1 root root 20694 2012-06-04 13 09 postgres.h drwxr-xr-x 2 root root 4096 2011-09-29 20 51 utils/ テストデータ作成 ユーザテーブル作成 $ psql chapatidb chapatidb=# create table users (id bigint, name varchar(50), email varchar(50)); CREATE TABLE 読み込むテキスト作成 user.txt 値の間の空白はタブです $ cat users.txt 1ちゃぱてぃchapati@example.com 2ぶるーべれーblue@example.com 3かぼちゃパイkabocha@example.com テキスト読み込み \copyコマンドで、テキストファイルをDBに読み込めるようです。 $ psql chapatidb chapatidb=# \copy users from users.txt 読み込み結果確認 chapatidb=# select * from users; id | name | email ----+--------------+--------------------- 1 | ちゃぱてぃ | chapati@example.com 2 | ぶるーべれー | blue@example.com 3 | かぼちゃパイ | kabocha@example.com (3 rows) テストプログラム作成、コンパイル、実行 pgtest.c作成 pgtest.c PostgreSQLをプログラムで操作する http //www.atmarkit.co.jp/flinux/rensai/postgres03/postgres03.html#3 上記のC言語サンプルをテーブル名等リテラルだけ変更しました。 /* ヘッダファイル取り込み */ #include stdio.h #include stdlib.h #include string.h #include errno.h #include "postgres.h" #include "libpq-fe.h" /* main処理 */ int main(int argc,char **argv) { /* 変数定義 */ char dbName[255] = "chapatidb"; /* データベース名はハードコーディング */ char sql[255]; int i; PGconn *con; PGresult *res; char *kou1,*kou2,*kou3; /* DBとの接続 */ con = PQsetdb("","",NULL,NULL,dbName); if ( PQstatus(con) == CONNECTION_BAD ) { /* 接続が失敗したときのエラー処理 */ fprintf(stderr,"Connection to database %s failed.\n",dbName); fprintf(stderr,"%s",PQerrorMessage(con)); exit(1); } /* select文の発行 */ sprintf(sql,"select * from users"); res = PQexec(con,sql); if (PQresultStatus(res) != PGRES_TUPLES_OK) { /* SQLの実行に失敗したときのエラー処理 */ fprintf(stderr,"%s",PQerrorMessage(con)); exit(1); } printf("id name email\n"); printf("--------------------------------------\n"); for(i = 0; i 3 ;i++) { kou1 = PQgetvalue(res,i,0); kou2 = PQgetvalue(res,i,1); kou3 = PQgetvalue(res,i,2); printf("%s %s %s\n",kou1,kou2,kou3); } PQclear(res); } コンパイル -Iオプションでヘッダファイルの場所を指定して、-lpqでPostgreSqlライブラリの使用を明示しているのかな? $ gcc pgtest.c -I/usr/include/postgresql -lpq 実行 すごいです、動きました。 $ ./a.out id name email -------------------------------------- 1 ちゃぱてぃ chapati@example.com 2 ぶるーべれー blue@example.com 3 かぼちゃパイ kabocha@example.com
https://w.atwiki.jp/estorage/pages/19.html
PostgreSQL導入設定
https://w.atwiki.jp/m_shige1979/pages/398.html
DB設定 プライマリサーバでデータベース用ディレクトリを作成 [root@test_dev1 ~]# mkdir /data/pgsql [root@test_dev1 ~]# [root@test_dev1 ~]# chown postgres postgres /data/pgsql/ [root@test_dev1 ~]# データベースクラスタを作成 [postgres@test_dev1 ~]$ initdb --no-locale --encoding=UTF-8 /data/pgsql/data The files belonging to this database system will be owned by user postgres . This user must also own the server process. The database cluster will be initialized with locale C. The default text search configuration will be set to english . creating directory /data/pgsql/data ... ok creating subdirectories ... ok selecting default max_connections ... 100 selecting default shared_buffers/max_fsm_pages ... 32MB/204800 creating configuration files ... ok creating template1 database in /data/pgsql/data/base/1 ... ok initializing pg_authid ... ok initializing dependencies ... ok creating system views ... ok loading system objects descriptions ... ok creating conversions ... ok creating dictionaries ... ok setting privileges on built-in objects ... ok creating information schema ... ok vacuuming database template1 ... ok copying template1 to template0 ... ok copying template1 to postgres ... ok WARNING enabling trust authentication for local connections You can change this by editing pg_hba.conf or using the -A option the next time you run initdb. Success. You can now start the database server using postgres -D /data/pgsql/data or pg_ctl -D /data/pgsql/data -l logfile start [postgres@test_dev1 ~]$ 簡易稼動確認 [postgres@test_dev1 ~]$ pg_ctl -D /data/pgsql/data start server starting [postgres@test_dev1 ~]$ LOG database system was shut down at 2009-06-02 11 06 15 JST LOG autovacuum launcher started LOG database system is ready to accept connections [postgres@test_dev1 ~]$ psql Welcome to psql 8.3.7, the PostgreSQL interactive terminal. Type \copyright for distribution terms \h for help with SQL commands \? for help with psql commands \g or terminate with semicolon to execute query \q to quit postgres=# \q [postgres@test_dev1 ~]$ [postgres@test_dev1 ~]$ pg_ctl -D /data/pgsql/data stop [postgres@test_dev1 ~]$
https://w.atwiki.jp/a3mi22/pages/110.html
postgresの起動と停止 Admin権限っぽいユーザになって実行する。 #su postgres 起動 #./pg_ctl start 停止 #./pg_ctl stop 合計: -
https://w.atwiki.jp/skmt200x/pages/106.html
RHEL系OS(RPM/YUM)にてインストールする際のメモ。 RPMの入手postgresql-libs postgresql postgresql-server postgresql-contrib uuid インストール TOPへ編集 RPMの入手 PostgreSQLのパッケージは主に以下の4種類のパッケージが存在するので、RPMダウンロードサイトより入手する。 postgresql-libs PostgreSQLのシェアドライブラリを格納したパッケージ。 postgresql PostgreSQLのクライアントプログラムパッケージ。 postgresql-server PostgreSQLのサーバプログラムパッケージ。 postgresql-contrib PostgreSQLの関連ツールを含めたプログラムパッケージ。 oid2nameなどの、運用時に便利なツールを格納しているため、インストールした方が良い。 uuid 上記のcontribをインストールする際にossp-uuidパッケージが必要。 yum search uuid にて検索、インストールが可能。 インストール それぞれのRPMをインストールする。 最終更新日 [2014-12-27]
https://w.atwiki.jp/parepan/pages/115.html
当月末の日付を問い合わせる SQL文 SELECT TO_CHAR(( SELECT CAST(( SELECT TO_CHAR(( SELECT CAST(current_timestamp AS DATE) + CAST( 1 month AS INTERVAL)), YYYYMM ) || 01 AS DATE) AS DATE) - CAST( 1 DAY AS INTERVAL) ), YYYY/MM/DD ) AS LAST_DATE 解説 方法 1.システム日付の当日を問い合わせする。 2.当日の一ヶ月後が何年何月か問い合わせした結果の末尾に「01」という文字を結合し、 「来月の第一日」という内容に成形する。 3.来月の第一日より一日を引いた日付が何日かを問い合わせる 4.3の問い合わせ結果には時間が入っているので、「YYYY/MM/DD」に成形する。 これで当月末日が何日であるかを問い合わせる事ができました。 ※動作確認環境 PostgreSQL 8.2.4